package com.douban.utils
{
	import com.adobe.crypto.MD5;
	import com.adobe.crypto.SHA1;
	import com.douban.auth.DBAuthenticate;
	
	import de.popforge.utils.SprintfUtil;
	
	import flash.utils.getDefinitionByName;
	
	import net.sevear.chaos.download.Predef;
	import net.sevear.chaos.vo.TrackInfo;
	
	public class Format
	{
		public function Format()
		{
		}
		
		public static function signUrl(url:String):String{
			return url + 
				(url.indexOf("?") == -1 ? ("?") : ("&")) + 
				"r=" + SHA1.hash(url + "fr0d0").substr(-10);
		}
		
		public static function channelsUrl():String{
			if(DBAuthenticate.getInstance().logInType == DBAuthenticate.LOGIN_APP_TYPE){
				return SprintfUtil.sprintf(
					Predef.DOUBAN_APP_HOST+"/radio/channels?app_name=radio_iphone&version=20&user_id=%s&expire=%s&token=%s",
					DBAuthenticate.getInstance().dbUID,
					DBAuthenticate.getInstance().appExpire,
					DBAuthenticate.getInstance().appToken);
			}else if(DBAuthenticate.getInstance().logInType == DBAuthenticate.LOGIN_WEB_TYPE){
				//TODO:
				return "";
			}
			else return "";
		}
		
		public static function radioListURL():String{
			if(DBAuthenticate.getInstance().logInType == DBAuthenticate.LOGIN_APP_TYPE){
				return SprintfUtil.sprintf(
					Predef.DOUBAN_APP_HOST+"/radio/people?app_name=radio_iphone&version=20&user_id=%s&expire=%s&token=%s",
					DBAuthenticate.getInstance().dbUID,
					DBAuthenticate.getInstance().appExpire,
					DBAuthenticate.getInstance().appToken);
			}else if(DBAuthenticate.getInstance().logInType == DBAuthenticate.LOGIN_WEB_TYPE){
				return Predef.DOUBAN_FM_HOST+"/j/mine/playlist?";
			}
			else return "";
		}
		
		
		public static function msFormat(miliSec:int):String
		{
			var min:int = Math.floor(miliSec / 60000);
			var sec:int = Math.floor(miliSec / 1000) - min * 60;
			var minStr:String = min < 10 ? ("0" + min) : (min).toString();
			var secStr:String = sec < 10 ? ("0" + sec) : (sec).toString();
			return minStr + ":" + secStr;
		}
		
		public static function renderHtmlStr(content:String,color:uint,
											 size:int = 12,
											 isBold:Boolean=false,
											 isUnderline:Boolean = false
											 ):String{
			var str:String = "<font size='12' color='#"+color.toString(16)+"' size='"+size+"'>"+content+"</font>";
			if(isBold){
				str="<b>"+str+"<b />";
			}
			if(isUnderline){
				str="<u>"+str+"<u />";
			}
			return str;
		}
		
		public static function dateFormt(d:Date):String{
			var resultStr : String = "";
			resultStr += d.getFullYear( )+ "-"   
				+ fillwithZero(d.getMonth() + 1) + "-"
				+ fillwithZero(d.getDate()) + " "
				+ fillwithZero(d.getHours()) + ":"
				+ fillwithZero(d.getMinutes()) + ":"
				+ fillwithZero(d.getSeconds());					
			return resultStr;   		
		}
		
		private static function fillwithZero(value:int,width:int=2):String{
			var str:String= value.toString();
			for(var i:int=str.length;i<width;i++){
				str = "0"+str;
			}
			return str;
		}
		
		//YYYY-MM-DD HH:MM:SS
		public static function castStrToDate(dateString:String):Date { 
			if ( dateString == null ) { 
				return null; 
			} 
			
			var year:int = int(dateString.substr(0,4)); 
			var month:int = int(dateString.substr(5,2))-1; 
			var day:int = int(dateString.substr(8,2)); 
			
			if ( year == 0 && month == 0 && day == 0 ) { 
				return null; 
			} 
			
			if ( dateString.length == 10 ) { 
				return new Date(year, month, day); 
			} 
			
			var hour:int = int(dateString.substr(11,2)); 
			var minute:int = int(dateString.substr(14,2)); 
			var second:int = int(dateString.substr(17,2)); 
			
			return new Date(year, month, day, hour, minute, second); 
		} 
		
		public static function inviteURLFormat(uid:int,invitedId:int,track:TrackInfo=null):String{
			var url:String = SprintfUtil.sprintf("%s/invite?uid=%s&toid=%s",Predef.CHAOS_HOST,uid,invitedId);
			var trackArgs:String = "";
			if(track!=null){
				var configXML : XML = flash.utils.describeType(track);
				for (var i : int = 0;i < configXML.variable.length();i++) {
					var name : String = configXML.variable[i].@name;
					var type : String = configXML.variable[i].@type;
					trackArgs+=encodeURIComponent(name)+"|"+encodeURIComponent(track[name])+"|";
				}
				trace(trackArgs);
				url+="&track="+trackArgs;
			}
			
			return url;
		}
		
		public static function getEvtUserInfo(str:String):Array{
			var rst:Array = [];
			var m:Array = str.match(/\[type=user,id=[0-9]*,display=[^\]]*]/g);
			for each(var mItem:String in m){
				var uidMatchs:Array = mItem.match(/[0-9]+/);
				if(uidMatchs.length>0){
					var uid:int = parseInt(uidMatchs[0] as String);
					var name:String = mItem.slice(mItem.search("display=")+"display=".length,mItem.length-1);
					rst.push({uid:uid,name:name});
				}
			}
			return rst;
		}
		
		public static function getEvtTrackInfo(str:String):Array{
			var rst:Array = [];
			var m:Array = str.match(/\[type=track,sid=[0-9]*,display=[^\]]*]/g);
			for each(var mItem:String in m){
				var uidMatchs:Array = mItem.match(/[0-9]+/);
				if(uidMatchs.length>0){
					var sid:int = parseInt(uidMatchs[0] as String);
					var name:String = mItem.slice(mItem.search("display=")+"display=".length,mItem.length-1);
					rst.push({sid:sid,name:name});
				}
			}
			return rst;
		}
		
		public static function getEvtP2pSiteInfo(str:String):Array{
			var rst:Array = [];
			var m:Array = str.match(/\[type=radio,id=[0-9]*,display=[^\]]*]/g);
			for each(var mItem:String in m){
				var uidMatchs:Array = mItem.match(/[0-9]+/);
				if(uidMatchs.length>0){
					var sid:int = parseInt(uidMatchs[0] as String);
					var name:String = mItem.slice(mItem.search("display=")+"display=".length,mItem.length-1);
					rst.push({id:sid,name:name});
				}
			}
			return rst;
		}
	}
}